package com.gzc.just.play.last.war.centralserver.leaderboard;

/**
 * 排行榜条目 - 代表排行榜中的一条记录
 * 
 * @author AI编程助手
 * @version 1.0
 */
public class LeaderboardEntry implements Comparable<LeaderboardEntry> {
    
    private long playerId;
    private String playerName;
    private long score;
    private int rank = 0;
    private long lastUpdateTime;
    private int wins = 0;
    private int losses = 0;
    private int kills = 0;
    private int deaths = 0;
    private double winRate = 0.0;
    private double kda = 0.0;
    
    /**
     * 构造函数
     * 
     * @param playerId 玩家ID
     * @param playerName 玩家名称
     * @param score 分数
     */
    public LeaderboardEntry(long playerId, String playerName, long score) {
        this.playerId = playerId;
        this.playerName = playerName;
        this.score = score;
        this.lastUpdateTime = System.currentTimeMillis();
    }
    
    /**
     * 获取玩家ID
     * 
     * @return 玩家ID
     */
    public long getPlayerId() {
        return playerId;
    }
    
    /**
     * 获取玩家名称
     * 
     * @return 玩家名称
     */
    public String getPlayerName() {
        return playerName;
    }
    
    /**
     * 获取分数
     * 
     * @return 分数
     */
    public long getScore() {
        return score;
    }
    
    /**
     * 设置分数
     * 
     * @param score 新的分数
     */
    public void setScore(long score) {
        this.score = score;
        this.lastUpdateTime = System.currentTimeMillis();
    }
    
    /**
     * 增加分数
     * 
     * @param deltaScore 增加的分数
     */
    public void addScore(long deltaScore) {
        this.score += deltaScore;
        this.lastUpdateTime = System.currentTimeMillis();
    }
    
    /**
     * 获取排名
     * 
     * @return 排名
     */
    public int getRank() {
        return rank;
    }
    
    /**
     * 设置排名
     * 
     * @param rank 排名
     */
    public void setRank(int rank) {
        this.rank = rank;
    }
    
    /**
     * 获取最后更新时间
     * 
     * @return 时间戳（毫秒）
     */
    public long getLastUpdateTime() {
        return lastUpdateTime;
    }
    
    /**
     * 获取胜场数
     * 
     * @return 胜场数
     */
    public int getWins() {
        return wins;
    }
    
    /**
     * 增加胜场
     */
    public void addWin() {
        wins++;
        updateWinRate();
    }
    
    /**
     * 获取败场数
     * 
     * @return 败场数
     */
    public int getLosses() {
        return losses;
    }
    
    /**
     * 增加败场
     */
    public void addLoss() {
        losses++;
        updateWinRate();
    }
    
    /**
     * 获取击杀数
     * 
     * @return 击杀数
     */
    public int getKills() {
        return kills;
    }
    
    /**
     * 增加击杀数
     * 
     * @param count 增加的击杀数
     */
    public void addKills(int count) {
        kills += count;
        updateKDA();
    }
    
    /**
     * 获取死亡数
     * 
     * @return 死亡数
     */
    public int getDeaths() {
        return deaths;
    }
    
    /**
     * 增加死亡数
     * 
     * @param count 增加的死亡数
     */
    public void addDeaths(int count) {
        deaths += count;
        updateKDA();
    }
    
    /**
     * 获取胜率
     * 
     * @return 胜率（0.0-1.0）
     */
    public double getWinRate() {
        return winRate;
    }
    
    /**
     * 获取KDA（击杀/死亡/助攻）
     * 
     * @return KDA
     */
    public double getKDA() {
        return kda;
    }
    
    /**
     * 更新胜率
     */
    private void updateWinRate() {
        int totalGames = wins + losses;
        if (totalGames > 0) {
            winRate = (double) wins / totalGames;
        }
    }
    
    /**
     * 更新KDA
     */
    private void updateKDA() {
        if (deaths == 0) {
            kda = kills > 0 ? kills : 0.0;
        } else {
            kda = (double) kills / deaths;
        }
    }
    
    @Override
    public int compareTo(LeaderboardEntry other) {
        // 按分数从高到低排序
        if (this.score != other.score) {
            return Long.compare(other.score, this.score);
        }
        // 分数相同时，按胜率从高到低
        if (Math.abs(this.winRate - other.winRate) > 0.0001) {
            return Double.compare(other.winRate, this.winRate);
        }
        // 胜率也相同时，按玩家ID排序
        return Long.compare(this.playerId, other.playerId);
    }
    
    @Override
    public String toString() {
        return String.format("LeaderboardEntry{rank=%d, playerId=%d, name=%s, score=%d, wins=%d, losses=%d, winRate=%.2f%%}",
                           rank, playerId, playerName, score, wins, losses, winRate * 100);
    }
}
